/*-
 * Copyright (c) 2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ MTBLAS: mtblas_3_gmm.c    Rev. 0.0.0    2013-10-23T09:41+08:00 $
 */

#include <stdlib.h>
#ifdef TRACE_INFO
#include <stdio.h>
#endif

#include <bscerr.h>

#include <exec_isa.h>
#include <cor_ctrl.h>
#include <mtblas.h>

int mtblas_gemmf(size_t thrz, const int rdr, const int trna, const int trnb, const size_t rz, const size_t cz, const size_t kz, const float alpha, const float *ma, const size_t lda, const float *mb, const size_t ldb, const float beta, float *mc, const size_t ldc)
{
    int ierr = ERRNO_UNDEFINED;

    const step_t step_one = sizeof(float);
    const step_t step_fix = 0;
    const step_t step_lda = sizeof(float) * lda;
    const step_t step_incx = sizeof(float) * incx;

    const float buf_init = 0;

    size_t r_sz;  /* Length of result vector */
    size_t c_sz;  /* Length of FMAC */

    float *buf;  /* Buffer vector for storing ma * vx */

    operand_t *ra, *rb, *rc;  /* Operands */

    operand_t *op_a;  /* Point to target of executing and modifying operations execution pack */
    operand_t *op_b;  /* Point to target for modifying */
    operand_t *op_c;  /* Point to modifier of address */
    operand_t *op_d;  /* Point to modifier of step */

    opchunk_t *pk0a;  /* Target of executing and modifying operations execution pack */
    opchunk_t *pk0c;  /* Modifier of address */
    opchunk_t *pk0d;  /* Modifier of step */

    opchunk_t *pk1a;  /* Target of executing and modifying operations execution pack */
    opchunk_t *pk1c;  /* Modifier of address */
    opchunk_t *pk1d;  /* Modifier of step */

#ifdef TRACE_INFO
    printf("[F] mtblas_gemmf()\n");
#endif
    r_sz = rz;
    c_sz = cz;


    ierr = operands_create_3(thrz, &ra, &rb, &rc);

    buf = (float*) calloc(r_sz * c_sz, sizeof(float));

    /* buf[] = 0 */
    ierr = operands_batch_set(thrz, (void*) &buf_init, step_fix, ra);
    ierr = operands_distributor(thrz, r_sz, (void*) buf, step_one, rb);

    ierr = thread_execute(thrz, (void*) op2_copy_ff, r_sz, ra, rb, NULL, NULL);


    /* buf[] = ma * mb */


    /* mc = beta * mc + buf[] */


    /* Release Allocated Memory */
    ierr = operands_delete_3(ra, rb, rc);


#ifdef TRACE_INFO
    printf("    mtblas_gemmf() Passed!\n");
#endif

    return ierr;
}
